﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Forms;
using DataCentral.Dataset;

namespace DataCentral.UserControls
{
    public partial class DatabaseConfigControl : DataCentral.CommonCenterControl
    {
        public DatabaseConfigControl()
        {
            InitializeComponent();
        }

        private void DatabaseConfigControl_Load(object sender, EventArgs e)
        {
            daDBConfig.Fill(dsCentral.DatabaseConfiguration);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DBConfigurationForm frmDBConfiguration = new DBConfigurationForm(false, null, dsCentral, daDBConfig);
            if (frmDBConfiguration.ShowDialog()==DialogResult.OK)
                daDBConfig.Fill(dsCentral.DatabaseConfiguration);    
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            DataCentralDataset.DatabaseConfigurationRow drDatabase = (DataCentralDataset.DatabaseConfigurationRow)viewDatabase.GetDataRow(viewDatabase.FocusedRowHandle);
            DBConfigurationForm frmDBConfiguration = new DBConfigurationForm(true, drDatabase, dsCentral, daDBConfig);
            if (frmDBConfiguration.ShowDialog() == DialogResult.OK)
                daDBConfig.Fill(dsCentral.DatabaseConfiguration); 
        }

        private void viewDatabase_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataCentralDataset.DatabaseConfigurationRow drDatabase = (DataCentralDataset.DatabaseConfigurationRow)viewDatabase.GetDataRow(viewDatabase.FocusedRowHandle);
            btnEdit.Enabled = drDatabase != null;
        }

        private void viewDatabase_DoubleClick(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }
    }
}
